/* ------------------------------------------------------------------------
	Author: SlopeTracker (http://www.pluginexpert.com)
	Version: 1.0
------------------------------------------------------------------------- */
var stFacebookToolbox = {
    loadPage: function(action, data_post, message, callback_function) {        
        var post_data = "ajax=1&action="+action;
        
        if(data_post instanceof Array){
            stFacebookToolbox.dataPostFilter(data_post);
            for( var key in data_post){
                if(key == 'sortKey')
                    post_data +="&sort="+data_post[key];
                else
                    post_data +="&"+key+"="+data_post[key];
            }
        }
        //alert(post_data);
        jQuery.ajax({
            type: "POST",
            //url: "admin.php?page=home",
            data: post_data,
            cache: false,
            beforeSend: function(){
                if(jQuery("#loading").css('display') == 'none'){
                    jQuery.blockUI({
                        message: jQuery('#loading'),
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });
                //jQuery("#loading").fadeIn("fast");
                }
                jQuery("#inner_content").fadeOut("fast");
            }, //show loading just when link is clicked
            complete: function(){
                jQuery("#loading").fadeOut("fast");
                jQuery.unblockUI();
                if(action == 'applications'){
                    var string = jQuery("#default").val()
                    id = parseInt(string);
                    if(id > 0){
                        jQuery( "#accordion" ).accordion({
                            autoHeight: false,
                            navigation: true,
                            collapsible: true,
                            active: id
                        }); 
                    }
                    else
                    {
                        jQuery( "#accordion" ).accordion({
                            autoHeight: false,
                            navigation: true,
                            collapsible: true
                        }); 
                    }
                    
                }
            }, //stop showing loading when the process is complete
            success: function(data){
                jQuery("#inner_content").html(data);
                jQuery("#inner_content").append('<input type="hidden" value="'+action+'" id="current_action" />');
                if(message){
                    stFacebookToolbox.sendMessage(message);
                }
                if(callback_function){
                    callback_function.call(this, data_post);
                }
                jQuery("#inner_content").fadeIn("normal");
                if(jQuery("#check_chart").val() == 1){
                    drawVisualization();
                }
                
                
                
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });                
        stFacebookToolbox.pagingCallbackFunction = function(data) {};
        stFacebookToolbox.sortCallbackFunction = function(data) {};
    },

    load_tooltip: function(id){
        jQuery(document).ready(function() {
            if(id == '')
                jQuery("#inner_content").tooltip();
            else
                jQuery(id).tooltip();
        });
    },
    redirect: function(url){
      window.location = url;  
    },
    on_submitform: function(form, callback_function){
        jQuery(document).ready(function(){
            jQuery('.stform input').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery('.stform textarea').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery('.stform select').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery(form).validate({
                errorPlacement: function(error, element) {
                    //error.appendTo( element.parent());
                    element.parent().addClass('error');
                },
                submitHandler: function() {
                    jQuery.blockUI({
                        message: jQuery('#loading'),
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });
                    //jQuery("#loading").fadeIn("fast");
                    //jQuery("#inner_content").fadeOut("fast");
                    jQuery.post(jQuery(form).attr('action')+'&ajax=1', jQuery(form).serialize(),function(data, textStatus){
                        if(jQuery.isFunction(callback_function)){
                            callback_function.call(this, data);
                        }else if(typeof(callback_function) == 'undefined'){
                            stFacebookToolbox.sendMessage(data);
                        } else {
                            stFacebookToolbox.sendMessage(data);
                        }
                        if(jQuery("#loading").css('display') != 'none'){
                            jQuery.unblockUI();
                            jQuery("#loading").fadeOut('normal');
                        }
                    });
                }

            });
        });
    },

    load_main_menu: function(){
        jQuery(document).ready(function(){
            jQuery('#tabs li a').click(function(){
                var action = jQuery(this).attr('action');
                
                jQuery('#tabs ul li').each(function(){
                    var current_action = jQuery(this).children().attr('action');

                    if(current_action == action ){

                        jQuery(this).addClass('current');
                    }else{
                        jQuery(this).removeClass('current');
                    }
                });

                if(jQuery(this).hasClass('have_child')){
                    
                    jQuery('#side-nav ul').each(function(){
                        if(jQuery(this).attr('id') == action){
                            jQuery(this).fadeIn();
                        }else{
                            jQuery(this).css('display','none');
                        }
                    });
                    jQuery('#side-nav ul li').each(function(){
                        if(jQuery(this).children('a').attr('action') == action){
                            jQuery(this).addClass('current');
                        }else{
                            jQuery(this).removeClass('current');
                        }
                    });
                    
                    stFacebookToolbox.loadPage(action, null, null, function(){
                        jQuery('#inner_content').css('width','87%');
                        jQuery('#inner_content').css('float','right');
                        jQuery('#side-nav').fadeIn('normal');
                    });
                }else{

                    jQuery('#side-nav').fadeOut('normal');                    
                    stFacebookToolbox.loadPage(action, null, null, function(){
                        jQuery('#inner_content').css('width','98%');
                        jQuery('#inner_content').css('float','none');
                    });
                }
            });
        //alert(jQuery(window).width());
        });
    },

    load_side_menu: function(){
        jQuery(document).ready(function(){
            jQuery('#side-nav li a').click(function(){
                var action = jQuery(this).attr('action');
                stFacebookToolbox.loadPage(action);
                jQuery('#side-nav ul li').each(function(){
                    var current_action = jQuery(this).children().attr('action');

                    if(current_action == action ){

                        jQuery(this).addClass('current');
                    }else{
                        jQuery(this).removeClass('current');
                    }
                });
            });
            jQuery('#side-nav li a.have_child').click(function(){
                jQuery('#side-nav').fadeIn();
            });
        });
    },

    sortColumn: function(){
        jQuery(document).ready(function(){
            jQuery('th.manage-column').css('cursor', 'pointer');
            jQuery('th.manage-column').click(function(){
                var current_action = jQuery('#current_action').val();
                var sort_type = jQuery(this).attr('sort_type');
                var sort_id = jQuery(this).attr('id');
                if(sort_type == null){
                    sort_type = 'desc';
                }
                var data_post = new Array();
                
                stFacebookToolbox.sortCallbackFunction(data_post);
                
                
                data_post['sortKey'] = sort_id;
                data_post['sort_type'] = sort_type;
                jQuery('#st_pagination ul li').each(function(){
                    if(jQuery(this).hasClass('current')){
                        data_post['cur_page'] = jQuery(this).children().html();
                    }
                });
                stFacebookToolbox.loadPage(current_action, data_post, '', function(data_post){
                    if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                        jQuery('#'+data_post['sortKey']).attr('sort_type','asc');
                        jQuery('#'+data_post['sortKey']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
                    }else{
                        jQuery('#'+data_post['sortKey']).attr('sort_type','desc');
                        jQuery('#'+data_post['sortKey']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
                    }
                });
            });
        });
    },

    shortAction: function(){
        jQuery(document).ready(function(){
            jQuery('div.short_action span').css('cursor', 'pointer');
            jQuery('div.short_action span').click(function(){
                var current_action = jQuery('#current_action').val();
                var action = jQuery(this).attr('action');
                var short_action = jQuery(this).attr('short_action');
                var id = jQuery(this).attr('id');
                var confirm_message = jQuery(this).attr('confirm');
                var functionCall = jQuery(this).attr('function');
                var data_post = new Array(); 
                data_post['id'] = id;
                data_post['short_action'] = short_action;
                
                stFacebookToolbox.sortCallbackFunction(data_post);
                
                
                if(action){
                    current_action = action;
                }

                if(confirm_message){
                    stFacebookToolbox.confirmModal(confirm_message, short_action, data_post, functionCall);
                    
                }else{
                    if(functionCall == '' || functionCall == null){
                        if(id != ''){
                        stFacebookToolbox.loadPage(current_action, data_post);
                        }
                    }
                    else
                        eval(functionCall);
                }
            });
            
        });
    },

    sendMessage: function(data){
        jQuery(document).ready(function(){
            try{
                data = eval('('+data+')');
            }catch (e){}
            
            if(typeof(data)=='string'){
                var message = data;
                var status = 'message';
                var title = 'title';
            }else{
                message = data['message'];
                status = data['status'];
                title = data['title'];
            }
            if(status == 'success'){
                if(title == '' || !title){title = 'Success!'}
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).addClass('notification_success');
                jQuery('.notification .text strong').html(title);
            }else if(status == 'message'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_message');
                jQuery('.notification .text strong').html('Message!');
            }else{
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_error');
                jQuery('.notification .text strong').html('Error!');
            }
            jQuery('.notification .text span').html(message);
            jQuery('.notification').fadeIn('slow');
            
            jQuery('.notification').click(function() {
                jQuery(this).fadeOut(800);
            });
            window.setTimeout(function(){
                jQuery('.notification').fadeOut();
            }, 2500);
        });

    },

    paging: function(page_num, action, callback_function){
        var data_post = new Array();
        data_post['cur_page'] = page_num;

        jQuery('th.manage-column').each(function(){
            if(jQuery(this).attr('sort_type')){
                //alert(jQuery(this).attr('sort_type'));
                data_post['sort'] = jQuery(this).attr('id');
                if(jQuery(this).attr('sort_type') == 'desc')
                    data_post['sort_type'] = 'asc';
                else
                    data_post['sort_type'] = 'desc';
            }
        });

        stFacebookToolbox.pagingCallbackFunction(data_post);

        stFacebookToolbox.loadPage(action, data_post, '', function(data_post){
            if(data_post['sort_type'] != null){
                if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                    jQuery('#'+data_post['sort']).attr('sort_type','asc');
                    jQuery('#'+data_post['sort']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;</span>');
                }else{
                    jQuery('#'+data_post['sort']).attr('sort_type','desc');
                    jQuery('#'+data_post['sort']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;</span>');
                }
            }
        });
    },
    pagingCallbackFunction: function(data){},
    sortCallbackFunction: function(data){},

    loadUploadButton: function(thumbnail_width, action, callBackfunction, thumbnail_height, postcard, idBtn){
        if(!action){
            action = 'ajaxPostAction';
        }
        if(!thumbnail_width){
            thumbnail_width = '';
        }
        
        if(!thumbnail_height){
            thumbnail_height = '';
        }
        
        jQuery(document).ready(function(){
            if(idBtn){
                jQuery(idBtn).click(function(){
                    var clickedObject = jQuery(this);
                    var clickedID = jQuery(this).attr('id');
                    new AjaxUpload(clickedID, {
                        action: location.href,
                        name: clickedID, // File upload name
                        data: { // Additional data to send
                            action: action,
                            ajax: 1,
                            type: 'upload',
                            data: clickedID,
                            thumbnail_width: thumbnail_width,
                            thumbnail_height: thumbnail_height,
                            post_card: postcard
                        },
                        autoSubmit: true, // Submit file after selection
                        responseType: false,
                        onChange: function(file, extension){},
                        onSubmit: function(file, extension){
                            clickedObject.text('Uploading'); // change button text, when user selects file
                            this.disable(); // If you want to allow uploading only 1 file at time, you can disable upload button
                            idBtn = window.setInterval(function(){
                                var text = clickedObject.text();
                                if (text.length < 13){
                                    clickedObject.text(text + '.');
                                }
                                else {
                                    clickedObject.text('Uploading');
                                }
                            }, 200);
                        },
                        onComplete: function(files, response) {
                            response = response.split('|*|');
                            var file_url = response[0];
                            var file = response[1];
                            var buildReturn
                            window.clearInterval(idBtn);
                            clickedObject.text('Choose graphics');
                            this.enable(); // enable upload button

                            // If there was an error
                            if(file_url.search('Upload Error') > -1){
                                buildReturn = '<span class="upload-error">' + file_url + '</span>';
                                jQuery(".upload-error").remove();
                                clickedObject.parent().after(buildReturn);

                            }
                            else{
                                if(jQuery.isFunction(callBackfunction)){
                                    callBackfunction(thumbnail_width, file_url, clickedID, clickedObject, file);
                                } else {
                                    file_new_url = file_url.substr(0,file_url.length-4)+'-thumb'+file_url.substr(-4,4);
    //                                file_new_url = file_url;
                                    buildReturn = '<img class="hide preview" id="image_'+clickedID+'" src="'+file_new_url+'" alt="" />';
                                    jQuery(".upload-error").remove();
                                    jQuery("#image_" + clickedID).remove();
                                    clickedObject.parent().after(buildReturn);
                                    //document.getElementById('image_image').src = '';
                                    //document.getElementById('image_image').src = file_url;
                                    jQuery('img#image_'+clickedID).fadeIn();
                                    jQuery('img#image_'+clickedID).imgAreaSelect({
                                        handles: true, 
                                        onSelectChange: function(img, selection){
                                            jQuery("#x1").val(selection.x1);
                                            jQuery("#x2").val(selection.x2);
                                            jQuery("#y1").val(selection.y1);
                                            jQuery("#y2").val(selection.y2);
                                            jQuery("#width").val(selection.width);
                                            jQuery("#height").val(selection.height);
                                        }
                                    });
                                    clickedObject.next('span').fadeIn();
                                    clickedObject.parent().prev('input').val(file);
                                }
                            }
                        }
                    });

                });
            }else{
                jQuery('.image_upload_button').each(function(){
                    var clickedObject = jQuery(this);
                    var clickedID = jQuery(this).attr('id');
                    new AjaxUpload(clickedID, {
                        action: location.href,
                        name: clickedID, // File upload name
                        data: { // Additional data to send
                            action: action,
                            ajax: 1,
                            type: 'upload',
                            data: clickedID,
                            thumbnail_width: thumbnail_width,
                            thumbnail_height: thumbnail_height,
                            post_card: postcard
                        },
                        autoSubmit: true, // Submit file after selection
                        responseType: false,
                        onChange: function(file, extension){},
                        onSubmit: function(file, extension){
                            clickedObject.text('Uploading'); // change button text, when user selects file
                            this.disable(); // If you want to allow uploading only 1 file at time, you can disable upload button
                            interval = window.setInterval(function(){
                                var text = clickedObject.text();
                                if (text.length < 13){
                                    clickedObject.text(text + '.');
                                }
                                else {
                                    clickedObject.text('Uploading');
                                }
                            }, 200);
                        },
                        onComplete: function(files, response) {
                            response = response.split('|*|');
                            var file_url = response[0];
                            var file = response[1];
                            var buildReturn
                            window.clearInterval(interval);
                            if(file_url.substr(-4,4) == '.pdf')
                                 clickedObject.text('Choose PDF');
                            else
                                 clickedObject.text('Choose graphics');
                            this.enable(); // enable upload button

                            // If there was an error
                            if(file_url.search('Upload Error') > -1){
                                buildReturn = '<span class="upload-error">' + file_url + '</span>';
                                jQuery(".upload-error").remove();
                                clickedObject.parent().after(buildReturn);

                            }
                            else{
                                if(jQuery.isFunction(callBackfunction)){
                                    callBackfunction(thumbnail_width, file_url, clickedID, clickedObject, file);
                                } else {
                                    if(file_url.substr(-4,4) == '.pdf'){
                                        buildReturn = '<a href="'+file_url+'" id="image_'+clickedID+'" alt="" target="_blank">View PDF</a>';
                                    }
                                    else {
                                          file_new_url = file_url.substr(0,file_url.length-4)+'-thumb'+file_url.substr(-4,4);
        //                                file_new_url = file_url;
                                          buildReturn = '<img class="hide preview" id="image_'+clickedID+'" src="'+file_new_url+'" alt="" />';
                                    }
                                    
                                    jQuery(".upload-error").remove();
                                    jQuery("#image_" + clickedID).remove();
                                    clickedObject.parent().after(buildReturn);
                                    //document.getElementById('image_image').src = '';
                                    //document.getElementById('image_image').src = file_url;
                                    jQuery('img#image_'+clickedID).fadeIn();
                                    jQuery('img#image_'+clickedID).imgAreaSelect({
                                        handles: true, 
                                        onSelectChange: function(img, selection){
                                            jQuery("#x1").val(selection.x1);
                                            jQuery("#x2").val(selection.x2);
                                            jQuery("#y1").val(selection.y1);
                                            jQuery("#y2").val(selection.y2);
                                            jQuery("#width").val(selection.width);
                                            jQuery("#height").val(selection.height);
                                        }
                                    });
                                    clickedObject.next('span').fadeIn();
                                    clickedObject.parent().prev('input').val(file);
                                }
                            }
                        }
                    });

                });
            }
        });
    },
    loadRemoveButton: function(action){
        if(!action){
            action = 'ajaxPostAction';
        }
        jQuery(document).ready(function(){
            jQuery('.image_reset_button').click(function(){
                var clickedObject = jQuery(this);
                var theID = jQuery(this).attr('pictureID');

                var ajax_url = location.href;

                var data = {
                    action: action,
                    ajax: 1,
                    type: 'image_remove',
                    data: theID,
                    file: jQuery('#id_'+theID).val()
                };

                jQuery.post(ajax_url, data, function(response) {
                    var image_to_remove = jQuery('#image_' + theID);
                    var button_to_hide = jQuery('#reset_' + theID);
                    image_to_remove.fadeOut(500,function(){
                        jQuery(this).remove();
                    });
                    button_to_hide.fadeOut();
                    clickedObject.parent().prev('input').val('');
                });
                return false;

            });
        });
    },

    showSideMenu: function(action){
        jQuery(document).ready(function(){
            jQuery('#side-nav').fadeIn();
            jQuery('#side-nav ul').each(function(){
                jQuery(this).css('display','none');
            });
            jQuery('#side-nav ul li').each(function(){
                if(jQuery(this).children('a').attr('action') == action){
                    jQuery(this).addClass('current');
                    jQuery(this).parent('ul').fadeIn();
                }else{
                    jQuery(this).removeClass('current');
                }
            });

            jQuery('#inner_content').css('width','87%');
            jQuery('#inner_content').css('float','right');
        });
    },
    confirmModal: function(message, btnText, data_post, callbackFunction){
        jQuery( "#dialog-confirm" ).html("<p>"+message+"</p>");
        jQuery( "#dialog-confirm" ).dialog({
            title: 'Confirm',
            resizable: false,
            modal: true,
            show: "slide",
            hide: "slide",
            buttons: [
            {
                text: btnText,
                click: function() {
                    jQuery( this ).dialog( "close" );
                    var current_action = jQuery('#current_action').val();
                    if(callbackFunction == '' || callbackFunction == null)
                        stFacebookToolbox.loadPage(current_action, data_post);
                    else
                        eval(callbackFunction);
                }
            },
            {
                text: "Cancel",
                click: function() {
                    jQuery( this ).dialog( "close" );
                }
            }
            ]
        });        
    },
    dataPostFilter: function(data_post){}
};